package jt.classic.system.workflow.impl;

import jt.classic.system.ISystem;
import jt.classic.system.workflow.WFlow;
import jt.classic.system.workflow.WHandle;
import jt.classic.system.workflow.WStep;
import jt.classic.system.workflow.WTrend;

import org.dom4j.Document;
import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.limp.basework.AbstractSimpleBean;

import org.limp.mine.StringTool;

import java.io.StringReader;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class WStepImpl extends AbstractSimpleBean implements WStep {
    private String id;
    private String name;
    private String memo;
    private int status;
    private WFlow flow;
    private String[] lastid;
    private String[] nextid;
    private int inner;
    private int no;
    private int para;
    private int cc;
    private int paravalue;
    private String rangeids;
    private String rangenames;
    private int rangetype;
    private int scorr;
    private int xCoordinate;
    private int yCoordinate;
    private int type;
    private String plugxml;
    private List trend;
    private List handle;
    private List introplugs = new ArrayList();
    private List outroplugs = new ArrayList();
    private List importplugs = new ArrayList();
    private List exportplugs = new ArrayList();
    private int senders;
    private boolean trans;

    public WStepImpl() {
    }

    public WStepImpl(WFlow flow) {
        this.flow = flow;
    }

    public String id() {
        // TODO Auto-generated method stub
        return this.id;
    }

    public String imemo() {
        return this.memo;
    }

    public int istatus() {
        return this.status;
    }

    public int icc() {
        return this.cc;
    }

    public int irangetype() {
        return this.rangetype;
    }

    public WFlow iflow() {
        // TODO Auto-generated method stub
        return this.flow;
    }

    public String[] ilastids() {
        // TODO Auto-generated method stub
        return this.lastid;
    }

    public String iname() {
        // TODO Auto-generated method stub
        return this.name;
    }

    public int inner() {
        // TODO Auto-generated method stub
        return this.inner;
    }

    public int ino() {
        // TODO Auto-generated method stub
        return this.no;
    }

    public int ixCoordinate() {
        return this.xCoordinate;
    }

    public int iyCoordinate() {
        return this.yCoordinate;
    }

    public String irangeids() {
        return this.rangeids;
    }

    public String irangenames() {
        return this.rangenames;
    }

    public int ipara() {
        // TODO Auto-generated method stub
        return this.para;
    }

    public int iparavalue() {
        return this.paravalue;
    }

    public int iscorr() {
        // TODO Auto-generated method stub
        return this.scorr;
    }

    public int itype() {
        // TODO Auto-generated method stub
        return this.type;
    }

    public List itrend() {
        return this.trend;
    }

    public boolean ifork() {
        return (this.trend != null) && !this.trend.isEmpty() &&
        (this.trend.size() > 1);
    }

    public boolean ijion() {
        return (this.lastid != null) && (this.lastid.length > 1);
    }

    public WHandle ihandle(String tablename, String itemname) {
        WHandle h = null;

        if ((this.handle != null) && !this.handle.isEmpty()) {
            for (int i = 0; i < this.handle.size(); i++) {
                WHandle temp = (WHandle) this.handle.get(i);

                if (temp.itemtable().getName().equals(tablename) &&
                        temp.item().equals(itemname)) {
                    h = temp;

                    break;
                }
            }
        }

        return h;
    }

    public WHandle iMAddhandle(String tablename) {
        WHandle h = null;

        if ((this.handle != null) && !this.handle.isEmpty()) {
            for (int i = 0; i < this.handle.size(); i++) {
                WHandle temp = (WHandle) this.handle.get(i);

                if (temp.itemtable().getName().equals(tablename) &&
                        temp.item().equals("MADDTYPE")) {
                    h = temp;

                    break;
                }
            }
        }

        return h;
    }

    public WHandle iMDelhandle(String tablename) {
        WHandle h = null;

        if ((this.handle != null) && !this.handle.isEmpty()) {
            for (int i = 0; i < this.handle.size(); i++) {
                WHandle temp = (WHandle) this.handle.get(i);

                if (temp.itemtable().getName().equals(tablename) &&
                        temp.item().equals("MDELTYPE")) {
                    h = temp;

                    break;
                }
            }
        }

        return h;
    }

    public String iplugXML() {
        return this.plugxml;
    }

    public List iplug4intro() {
        return this.introplugs;
    }

    public List iplug4outro() {
        return this.outroplugs;
    }

    public List iplug4import() {
        return this.importplugs;
    }

    public List iplug4export() {
        return this.exportplugs;
    }

    /**
     * 初始化分析插件xml脚本
     * @param xml
     */
    private void initPlugs(String xml) {
        SAXReader reader = new SAXReader();

        try {
            Document document = reader.read(new StringReader(xml));

            /*object元素*/
            Iterator iter = document.getRootElement().elementIterator("plug");

            while (iter.hasNext()) {
                Element element = (Element) iter.next();

                String tp = StringTool.checkString(element.attributeValue(
                            "type"));

                String className = StringTool.checkString(element.attributeValue(
                            "className"));

                if (tp.equals("intro") && !className.equals("")) {
                    this.introplugs.add(className);
                } else if (tp.equals("outro") && !className.equals("")) {
                    this.outroplugs.add(className);
                } else if (tp.equals("import") && !className.equals("")) {
                    this.importplugs.add(className);
                } else if (tp.equals("export") && !className.equals("")) {
                    this.exportplugs.add(className);
                }
            }
        } catch (Exception ex) {
            ISystem.catchException(ex);
        }
    }

    public int isenders() {
        return this.senders;
    }

    public boolean itrans() {
        return this.trans;
    }

    public void reload(Map info) {
        super.reload(info);
        this.id = (String) info.get("S_ID");
        this.name = (String) info.get("S_NAME");
        this.memo = (String) info.get("S_MEMO");
        this.no = Integer.parseInt(StringTool.checkString(info.get("S_NO"), "0"));
        this.type = Integer.parseInt(StringTool.checkString(info.get("S_TYPE"),
                    "0"));
        this.rangetype = Integer.parseInt(StringTool.checkString(info.get(
                        "S_RANGETYPE"), "0"));
        this.rangeids = (String) info.get("S_ROLEID");
        this.rangenames = (String) info.get("S_ROLENAME");
        this.scorr = Integer.parseInt(StringTool.checkString(info.get("S_CORR"),
                    "0"));
        this.inner = Integer.parseInt(StringTool.checkString(info.get("S_INNER"),
                    "0"));
        this.para = Integer.parseInt(StringTool.checkString(info.get("S_PARA"),
                    "0"));
        this.cc = Integer.parseInt(StringTool.checkString(info.get("S_CC"), "0"));
        this.plugxml = (String) info.get("PLUGXML");
        this.trans = "1".equals(info.get("S_TRANS"));
        this.senders = Integer.parseInt((String) info.get("S_SENDERS"));
        this.status = Integer.parseInt(StringTool.checkString(info.get(
                        "S_STATUS"), "0"));

        if (!StringTool.checkString(this.plugxml).equals("")) {
            initPlugs(this.plugxml);
        }

        this.lastid = (String[]) info.get("S_LASTID");
        this.trend = (List) info.get("TRENDS");
        this.handle = (List) info.get("HANDLES");

        String _xCoordinate = StringTool.checkString(info.get("S_XC"), "0");

        this.xCoordinate = Integer.parseInt(_xCoordinate);

        String _yCoordinate = StringTool.checkString(info.get("S_YC"), "0");

        this.yCoordinate = Integer.parseInt(_yCoordinate);
    }
}
